package com.jd.org.controller;

import com.jd.basic.utils.AjaxResult;
import com.jd.basic.utils.PageList;
import com.jd.org.domain.Employee;
import com.jd.org.query.EmployeeQuery;
import com.jd.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小林同学
 * @version 1.0
 * @description: TODO
 * @date 2023/2/18 21:01
 */
@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }
    /**
     * 根据id查询部门
     * @author kabuda
     * @date 2023/2/21 12:50
     * @return Employee
    */

    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id) {
        return employeeService.findById(id);
    }
    /**
     * 增加和修改
     * @author kabuda
     * @date 2023/2/21 17:53
     * @return AjaxResult
    */

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (null == employee.getId()) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }
    /**
     * 删除
     * @author kabuda
     * @date 2023/2/21 17:58
     * @return AjaxResult
    */


    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            employeeService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id ){
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping
    public PageList queryByPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryByPage(employeeQuery);
    }

    @GetMapping("/title/{departmentId}")
    public List<Employee> getEmpByDeptId(@PathVariable("departmentId") Long departmentId ) {
        return employeeService.getEmpByDeptId(departmentId);
    }

    @GetMapping("list/{shopId}")
    public List<Employee> getEmployees(@PathVariable("shopId") Long shopId){
        return employeeService.getEmployees(shopId);
    }
}
